/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.bean.db;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import org.opu.vcforms.bean.Template;
import org.opu.vcforms.resource.ResourceHandle;

/**
 *
 * @author yura
 */
public final class ConfigPath {
    /** Name of the L&F property in {@link System#getProperty(String)}*/
    public static final String LAF_PROPERTIES = "vcforms.laf";
    /** Name of the locale property in {@link System#getProperty(String)}*/
    public static final String LOCALE_PROPERTIES = "locale";
    /** Name of the debug property in {@link System#getProperty(String)}*/
    public static final String DEBUG = "debug";
    /** Name of the no error property in {@link System#getProperty(String)}*/
    public static final String NOW_ERROR_PROPERTIES = "vcforms.no_error";

    public static final String FILE_DIR = "file";
    public static final String HISTORY_DIR = "history";

    private static final ConfigPath instance = new ConfigPath();

    private final String os = System.getProperty("os.name");
    private final String userHome = System.getProperty("user.home");
    private final String language = System.getProperty("user.language");
    private final String tmp = System.getProperty("java.io.tmpdir");

    private final File applicationConfDir;
    private final Locale locale;

    private ConfigPath(){
        // init applicationConfDir
        String s = System.getProperty("file.separator");
        applicationConfDir = new File(userHome + s
                + (isWindows()? "": ".")
                + ResourceHandle.getProperty(ResourceHandle.APPLICATION_NAME)
                + s
                + ResourceHandle.getProperty(ResourceHandle.APPLICATION_VERSION));

        // init locale
        String _language = this.language;
        String localeName = System.getProperty(LOCALE_PROPERTIES);

        if (localeName != null){
            _language = localeName;
        }

        locale = new Locale(_language);
    }

    public static ConfigPath getInstance() {
        return instance;
    }


    public boolean isWindows(){
        return os.startsWith("Windows");
    }

    public String getLanguage() {
        return language;
    }

    public Locale getLocale(){
        return locale;
    }

    public String getOs() {
        return os;
    }

    public String getTmp() {
        return tmp;
    }

    public File getTmpFile() {
        return new File (getTmp());
    }

    public File getTmpFileForPrint(){
        return new File (getTmpFile(), "vc-forms-print");
    }

    public String getUserHome() {
        return userHome;
    }

    public File getApplicationConfDir() {
        return applicationConfDir;
    }

    public File getFrameProp(){
        return new File(applicationConfDir, "window.conf");
    }

    public File getGUIProp(){
        return new File(applicationConfDir, "gui.conf");
    }

    public File getUserDbProp(){
        return new File(applicationConfDir, "user.conf");
    }

    public File getTemplateDbProp(){
        return new File(applicationConfDir, "template.conf");
    }

    public File getWhereToStoring(Template tmplt, int course, List<String>grups){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());

        File file = new File(applicationConfDir, FILE_DIR);
        File tmltDr = new File(file, String.valueOf(tmplt));
        File year = new File(tmltDr, String.valueOf(calendar.get(Calendar.YEAR)));
        File month = new File(year, String.valueOf(calendar.get(Calendar.MONTH) + 1));
        File courseDr = new File(month, String.valueOf(course));

        File target = new File(courseDr, String.valueOf(grups));
        target.getParentFile().mkdirs();

        return target;
    }
}
